home *** CD-ROM | disk | FTP | other *** search
/ Programming a Multiplayer FPS in DirectX / Programming a Multiplayer FPS in DirectX (Companion CD).iso / DirectX / dxsdk_oct2004.exe / dxsdk.exe / Samples / C++ / Direct3D / StateManager / StateManagerApp.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2004-09-27  |  41.0 KB  |  915 lines

  1. //--------------------------------------------------------------------------------------
  2. // File: StateManagerApp.cpp
  3. //
  4. // This sample shows an example implementation of the ID3DXEffectStateManager interface.
  5. // This inteface can be used to implement custom state-change handling for the D3DX
  6. // Effects system.
  7. //
  8. // Copyright (c) Microsoft Corporation. All rights reserved.
  9. //--------------------------------------------------------------------------------------
  10. #include "dxstdafx.h"
  11. #include "resource.h"
  12.  
  13.  
  14. //--------------------------------------------------------------------------------------
  15. // Defines custom interface to a sample implementation of the ID3DXEffectStateManager
  16. // interface
  17. //--------------------------------------------------------------------------------------
  18. #include "EffectStateManager.h"
  19.  
  20.  
  21. //--------------------------------------------------------------------------------------
  22. // Contains (along with renderables.cpp) support code for sample.  This code is used
  23. // to manage Mesh, Effect, and Texture resources in a general manner.  Because elements
  24. // of this sample have performance implications (eg, redundant state filtering on
  25. // PURE device), it is desirable to handle renderable objects (mesh subsets) in a
  26. // such a general manner.  This allows lists of renderables to be re-sorted in arbitrary
  27. // orders, to minimize the number of state changes required to render the scene.
  28. //--------------------------------------------------------------------------------------
  29. #include "renderables.h"
  30.  
  31.  
  32. //--------------------------------------------------------------------------------------
  33. // The scene is loaded from an x-file, that has been extended to include templates for
  34. // specifying mesh filenames, and cameras.
  35. //--------------------------------------------------------------------------------------
  36. #include "LoadSceneFromX.h"
  37.  
  38.  
  39.  
  40. //--------------------------------------------------------------------------------------
  41. // Global variables
  42. //--------------------------------------------------------------------------------------
  43. ID3DXFont*              g_pFont = NULL;             // Font for drawing text
  44. ID3DXSprite*            g_pSprite = NULL;           // Sprite for batching draw text calls
  45. bool                    g_bShowHelp = true;         // If true, it renders the UI control text
  46. CFirstPersonCamera      g_Camera;                   // A model viewing camera
  47. CDXUTDialog             g_HUD;                      // manages the 3D UI
  48. CDXUTDialog             g_SampleUI;                 // dialog for sample specific controls
  49. CStateManagerInterface* g_pStateManager = NULL;     // The current ID3DXEffectStateManager implementation
  50. bool                    g_bSortByMaterial = true;   // Sort by Material/Effect (versus mesh instance)
  51. bool                    g_bSortOrderDirty = true;   // Causes the Renderable Objects to be re-sorted
  52. bool                    g_bAppIsDebug = false;      // The application build is debug
  53. bool                    g_bRuntimeDebug = false;    // The D3D Debug Runtime was detected
  54. bool                    g_bStateManger  = true;     // Enable custom-implemented d3dx effect state management
  55. bool                    g_bResetCamera = true;      // Causes camera to be (re)loaded from scene
  56. vector<CInstance*>      g_vecInstances;             // Contains the group of mesh instances composing the scene
  57. typedef vector<CRenderable> pass;                   // A render-pass is composed of individual items to be rendered
  58. vector<pass>            g_passes;                   // Contains a group of rendering passes
  59. UINT                    g_nBindCPU = 0;             // Add extra CPU workload, to (hopefully) become CPU-bound if not already
  60. UINT                    g_nMaxRocksToRender = 200;  // The initial maximum number of rocks to be rendered
  61.  
  62. //--------------------------------------------------------------------------------------
  63. // UI control IDs
  64. //--------------------------------------------------------------------------------------
  65. #define IDC_TOGGLEFULLSCREEN    1
  66. #define IDC_TOGGLEREF           3
  67. #define IDC_CHANGEDEVICE        4
  68. #define IDC_NUM_ROCKS_STATIC    5
  69. #define IDC_NUM_ROCKS           6
  70. #define IDC_TOGGLEPURE          7
  71. #define IDC_STATEMANAGER        8
  72. #define IDC_MATERIALSORT        9
  73. #define IDC_BIND_CPU_STATIC     10
  74. #define IDC_BIND_CPU            11
  75.  
  76.  
  77. //--------------------------------------------------------------------------------------
  78. // Forward declarations 
  79. //--------------------------------------------------------------------------------------
  80. bool    CALLBACK IsDeviceAcceptable( D3DCAPS9* pCaps, D3DFORMAT AdapterFormat, D3DFORMAT BackBufferFormat, bool bWindowed );
  81. void    CALLBACK ModifyDeviceSettings( DXUTDeviceSettings* pDeviceSettings, const D3DCAPS9* pCaps );
  82. HRESULT CALLBACK OnCreateDevice( IDirect3DDevice9* pd3dDevice, const D3DSURFACE_DESC* pBackBufferSurfaceDesc );
  83. HRESULT CALLBACK OnResetDevice( IDirect3DDevice9* pd3dDevice, const D3DSURFACE_DESC* pBackBufferSurfaceDesc );
  84. void    CALLBACK OnFrameMove( IDirect3DDevice9* pd3dDevice, double fTime, float fElapsedTime );
  85. void    CALLBACK OnFrameRender( IDirect3DDevice9* pd3dDevice, double fTime, float fElapsedTime );
  86. LRESULT CALLBACK MsgProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, bool* pbNoFurtherProcessing );
  87. void    CALLBACK KeyboardProc( UINT nChar, bool bKeyDown, bool bAltDown  );
  88. void    CALLBACK OnGUIEvent( UINT nEvent, int nControlID, CDXUTControl* pControl );
  89. void    CALLBACK OnLostDevice();
  90. void    CALLBACK OnDestroyDevice();
  91.  
  92. void    InitApp();
  93. HRESULT BuildSceneFromX( LPDIRECT3DDEVICE9 pd3dDevice );
  94. void    RenderText( double fTime );
  95. void    QueueAndSortRenderables();
  96. void    SortRenderables();
  97. void    SetStateManager( );
  98. HRESULT CreateInstance( LPDIRECT3DDEVICE9 pDevice, LPCWSTR wszFileName,
  99.                         D3DXMATRIX* pWorld, UINT nRenderPass, CInstance** ppInstance = NULL );
  100.  
  101.  
  102. //--------------------------------------------------------------------------------------
  103. // Entry point to the program. Initializes everything and goes into a message processing 
  104. // loop. Idle time is used to render the scene.
  105. //--------------------------------------------------------------------------------------
  106. INT WINAPI WinMain( HINSTANCE, HINSTANCE, LPSTR, int )
  107. {
  108.     try
  109.     {
  110.         // Set the callback functions. These functions allow the sample framework to notify
  111.         // the application about device changes, user input, and windows messages.  The 
  112.         // callbacks are optional so you need only set callbacks for events you're interested 
  113.         // in. However, if you don't handle the device reset/lost callbacks then the sample 
  114.         // framework won't be able to reset your device since the application must first 
  115.         // release all device resources before resetting.  Likewise, if you don't handle the 
  116.         // device created/destroyed callbacks then the sample framework won't be able to 
  117.         // recreate your device resources.
  118.         DXUTSetCallbackDeviceCreated( OnCreateDevice );
  119.         DXUTSetCallbackDeviceReset( OnResetDevice );
  120.         DXUTSetCallbackDeviceLost( OnLostDevice );
  121.         DXUTSetCallbackDeviceDestroyed( OnDestroyDevice );
  122.         DXUTSetCallbackMsgProc( MsgProc );
  123.         DXUTSetCallbackKeyboard( KeyboardProc );
  124.         DXUTSetCallbackFrameRender( OnFrameRender );
  125.         DXUTSetCallbackFrameMove( OnFrameMove );
  126.  
  127.         // Show the cursor and clip it when in full screen
  128.         DXUTSetCursorSettings( true, true );
  129.  
  130.         InitApp();
  131.  
  132.         // Initialize the sample framework and create the desired Win32 window and Direct3D 
  133.         // device for the application. Calling each of these functions is optional, but they
  134.         // allow you to set several options which control the behavior of the framework.
  135.         DXUTInit( true, true, true ); // Parse the command line, handle the default hotkeys, and show msgboxes
  136.         DXUTCreateWindow( L"StateManager" );
  137.         DXUTCreateDevice( D3DADAPTER_DEFAULT, true, 640, 480, IsDeviceAcceptable, ModifyDeviceSettings );
  138.  
  139.         // Performance observations should not be compared against dis-similar builds (debug v retail)
  140.     #if defined(DEBUG) | defined(_DEBUG)
  141.         g_bAppIsDebug = true;
  142.     #endif
  143.  
  144.         // Performance observations should not be compared against dis-similar d3d runtimes (debug v retail)
  145.         if( GetModuleHandleW( L"d3d9d.dll" ) )
  146.             g_bRuntimeDebug = true;
  147.  
  148.         // Pass control to the sample framework for handling the message pump and 
  149.         // dispatching render calls. The sample framework will call your FrameMove 
  150.         // and FrameRender callback when there is idle time between handling window messages.
  151.         DXUTMainLoop();
  152.  
  153.         // Perform any application-level cleanup here. Direct3D device resources are released within the
  154.         // appropriate callback functions and therefore don't require any cleanup code here.
  155.     }
  156.     catch( exception e )
  157.     {
  158.         WCHAR wsz[256];
  159.         MultiByteToWideChar( CP_ACP, 0, e.what(), -1, wsz, 256 );
  160.         wsz[ 255 ] = 0;
  161.         DXUTTrace( __FILE__, (DWORD)__LINE__, E_FAIL, wsz, true );
  162.     }
  163.     return DXUTGetExitCode();
  164. }
  165.  
  166.  
  167. //--------------------------------------------------------------------------------------
  168. // Initialize the app 
  169. //--------------------------------------------------------------------------------------
  170. void InitApp()
  171. {
  172.     // Set the application initial viewpoint
  173.     D3DXVECTOR3 vecEye(0.0f, 0.0f, 0.0f);
  174.     D3DXVECTOR3 vecAt (0.0f, 0.0f, 0.0f);
  175.     g_Camera.SetViewParams( &vecEye, &vecAt );
  176.  
  177.     // Set the camera speed
  178.     g_Camera.SetScalers( 0.01f, 10.0f );
  179.     
  180.     // Constrain the camera to movement within the horizontal plane
  181.     g_Camera.SetEnableYAxisMovement( false );
  182.  
  183.     // Initialize dialogs
  184.     g_HUD.SetCallback( OnGUIEvent ); int iY = 10; 
  185.     g_HUD.AddButton( IDC_TOGGLEFULLSCREEN, L"Toggle full screen", 35, iY, 125, 22 );
  186.     g_HUD.AddButton( IDC_CHANGEDEVICE, L"Change device (F2)", 35, iY += 24, 125, 22 );
  187.  
  188.     g_SampleUI.SetCallback( OnGUIEvent ); iY = 10; 
  189.  
  190.     WCHAR sz[100];
  191.     iY += 24;
  192.     _snwprintf( sz, 100, L"# Rocks: %d", g_nMaxRocksToRender ); sz[99] = 0;
  193.     g_SampleUI.AddStatic( IDC_NUM_ROCKS_STATIC, sz, 35, iY += 24, 125, 22 );
  194.     g_SampleUI.AddSlider( IDC_NUM_ROCKS, 50, iY += 24, 100, 22, 1, g_nMaxRocksToRender, g_nMaxRocksToRender );
  195.  
  196.     g_SampleUI.AddButton( IDC_TOGGLEPURE, L"Toggle PURE", 35, iY += 24, 125, 22 );
  197.     g_SampleUI.AddCheckBox( IDC_STATEMANAGER, L"Use State Manager", 35, iY += 24, 125, 22, g_bStateManger );
  198.     g_SampleUI.AddCheckBox( IDC_MATERIALSORT, L"Sort By Material", 35, iY += 24, 125, 22, g_bSortByMaterial );
  199.  
  200.     iY += 24;
  201.     _snwprintf( sz, 100, L"Bind CPU: %d", g_nBindCPU ); sz[99] = 0;
  202.     g_SampleUI.AddStatic( IDC_BIND_CPU_STATIC, sz, 35, iY += 24, 125, 22 );
  203.     g_SampleUI.AddSlider( IDC_BIND_CPU, 50, iY += 24, 100, 22, 0, 20, g_nBindCPU );
  204.  
  205. }
  206.  
  207.  
  208. //--------------------------------------------------------------------------------------
  209. // Called during device initialization, this code checks the device for some 
  210. // minimum set of capabilities, and rejects those that don't pass by returning E_FAIL.
  211. //--------------------------------------------------------------------------------------
  212. bool CALLBACK IsDeviceAcceptable( D3DCAPS9* pCaps, D3DFORMAT AdapterFormat, 
  213.                                   D3DFORMAT BackBufferFormat, bool bWindowed )
  214. {
  215.     // No fallback defined by this app, so reject any device that 
  216.     // doesn't support at least ps1.4
  217.     if( pCaps->PixelShaderVersion < D3DPS_VERSION(1,4) )
  218.         return false;
  219.  
  220.     // Skip devices that do not support cubemaps
  221.     if( 0 == (pCaps->TextureCaps & D3DPTEXTURECAPS_CUBEMAP ) )
  222.         return false;
  223.     
  224.     // Skip non-TL devices
  225.     if( 0 == (pCaps->DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT) )
  226.         return false;
  227.  
  228.     // Skip devices that do not enable PURE device
  229.     if (0 == (pCaps->DevCaps & D3DDEVCAPS_PUREDEVICE) )
  230.         return false;
  231.  
  232.     return true;
  233. }
  234.  
  235.  
  236. //--------------------------------------------------------------------------------------
  237. // This callback function is called immediately before a device is created to allow the 
  238. // application to modify the device settings. The supplied pDeviceSettings parameter 
  239. // contains the settings that the framework has selected for the new device, and the 
  240. // application can make any desired changes directly to this structure.  Note however that 
  241. // the sample framework will not correct invalid device settings so care must be taken 
  242. // to return valid device settings, otherwise IDirect3D9::CreateDevice() will fail.  
  243. //--------------------------------------------------------------------------------------
  244. void CALLBACK ModifyDeviceSettings( DXUTDeviceSettings* pDeviceSettings, const D3DCAPS9* pCaps )
  245. {
  246.     // This sample runs only against devices that support Pure Hardware TL.
  247.     pDeviceSettings->BehaviorFlags = D3DCREATE_HARDWARE_VERTEXPROCESSING;
  248.     pDeviceSettings->BehaviorFlags |= D3DCREATE_PUREDEVICE;
  249. }
  250.  
  251.  
  252. //--------------------------------------------------------------------------------------
  253. // This callback function will be called immediately after the Direct3D device has been 
  254. // created, which will happen during application initialization and windowed/full screen 
  255. // toggles. This is the best location to create D3DPOOL_MANAGED resources since these 
  256. // resources need to be reloaded whenever the device is destroyed. Resources created  
  257. // here should be released in the OnDestroyDevice callback. 
  258. //--------------------------------------------------------------------------------------
  259. HRESULT CALLBACK OnCreateDevice( IDirect3DDevice9* pd3dDevice, const D3DSURFACE_DESC* pBackBufferSurfaceDesc )
  260. {
  261.     HRESULT hr;
  262.  
  263.     // Initialize the font
  264.     V_RETURN( D3DXCreateFont( pd3dDevice, 15, 0, FW_BOLD, 0, FALSE, DEFAULT_CHARSET, 
  265.                               OUT_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, 
  266.                               L"Arial", &g_pFont ) );
  267.  
  268.     V_RETURN( BuildSceneFromX( pd3dDevice ) );
  269.  
  270.     // Establish a sort-order for the render queue
  271.     QueueAndSortRenderables();
  272.  
  273.     // Set a state manager for all loaded effects
  274.     SetStateManager();
  275.  
  276.     return S_OK;
  277. }
  278.  
  279.  
  280.  
  281. //--------------------------------------------------------------------------------------
  282. // This callback function will be called immediately after the Direct3D device has been 
  283. // reset, which will happen after a lost device scenario. This is the best location to 
  284. // create D3DPOOL_DEFAULT resources since these resources need to be reloaded whenever 
  285. // the device is lost. Resources created here should be released in the OnLostDevice 
  286. // callback. 
  287. //--------------------------------------------------------------------------------------
  288. HRESULT CALLBACK OnResetDevice( IDirect3DDevice9* pd3dDevice, 
  289.                                 const D3DSURFACE_DESC* pBackBufferSurfaceDesc )
  290. {
  291.     HRESULT hr;
  292.    
  293.     // Notify loaded resources that the device has been Created or Reset
  294.     V_RETURN( CTexture::ResetDevice() );
  295.     V_RETURN( CEffect::ResetDevice() );
  296.     V_RETURN( CMeshObject::ResetDevice() );
  297.  
  298.     if( g_pFont )
  299.         V_RETURN( g_pFont->OnResetDevice() );
  300.  
  301.     // Create a sprite to help batch calls when drawing many lines of text
  302.     V_RETURN( D3DXCreateSprite( pd3dDevice, &g_pSprite ) );
  303.  
  304.  
  305.     // Setup the camera's projection parameters
  306.     float fAspectRatio = pBackBufferSurfaceDesc->Width / (FLOAT)pBackBufferSurfaceDesc->Height;
  307.     g_Camera.SetProjParams( D3DX_PI/4, fAspectRatio, 0.1f, 500.f );
  308.  
  309.     g_HUD.SetLocation( pBackBufferSurfaceDesc->Width-170, 0 );
  310.     g_HUD.SetSize( 170, 170 );
  311.     g_SampleUI.SetLocation( pBackBufferSurfaceDesc->Width-170, pBackBufferSurfaceDesc->Height-300 );
  312.     g_SampleUI.SetSize( 170, 300 );
  313.  
  314.     // Device state does not persist across a reset.
  315.     // Any state that is cached within the state manager (for state filtering) will then
  316.     // become invalid. In turn, this can cause the state manager to initially filter some
  317.     // states that are mistakenly recognized as redundant.
  318.     // Reset the state manager by asking it to discard its cached states.
  319.     if( g_pStateManager )
  320.         g_pStateManager->DirtyCachedValues();
  321.  
  322.     return S_OK;
  323. }
  324.  
  325.  
  326. //--------------------------------------------------------------------------------------
  327. // This callback function will be called once at the beginning of every frame. This is the
  328. // best location for your application to handle updates to the scene, but is not 
  329. // intended to contain actual rendering calls, which should instead be placed in the 
  330. // OnFrameRender callback.  
  331. //--------------------------------------------------------------------------------------
  332. void CALLBACK OnFrameMove( IDirect3DDevice9* pd3dDevice, double fTime, float fElapsedTime )
  333. {
  334.     // Update the camera's position based on user input 
  335.     g_Camera.FrameMove( fElapsedTime );
  336.  
  337.     // If Extra CPU work is requested, add it
  338.     // State changes tend to involve significantly more CPU work than graphics hardware work.
  339.     // However, the application may be continually waiting on the graphics hardware.
  340.     // In this case, the overall application performance will not be greatly impacted until the
  341.     // CPU workload exceeds the graphics hardware workload.
  342.     D3DXMATRIX mat;
  343.     D3DXMatrixIdentity( &mat );
  344.     for( UINT i = 0; i< g_nBindCPU; i++ )
  345.         for( UINT n = 0; n < 10000; n++ )
  346.             D3DXMatrixMultiply( &mat, &mat, &mat );
  347.  
  348. }
  349.  
  350.  
  351. //--------------------------------------------------------------------------------------
  352. // This callback function will be called at the end of every frame to perform all the 
  353. // rendering calls for the scene, and it will also be called if the window needs to be 
  354. // repainted. After this function has returned, the sample framework will call 
  355. // IDirect3DDevice9::Present to display the contents of the next buffer in the swap chain
  356. //--------------------------------------------------------------------------------------
  357. void CALLBACK OnFrameRender( IDirect3DDevice9* pd3dDevice, double fTime, float fElapsedTime )
  358. {
  359.     HRESULT hr;
  360.     D3DXMATRIXA16 matProj, matView;
  361.     static UINT nFrameTimeStamp = 0;
  362.  
  363.     // Clear the render target and the zbuffer 
  364.     V( pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DXCOLOR(1.f,1.f,1.f,0.f), 1.0f, 0) );
  365.  
  366.     // Render the scene
  367.     if( SUCCEEDED( pd3dDevice->BeginScene() ) )
  368.     {
  369.         // Get the projection & view matrix from the camera class
  370.         matProj = *g_Camera.GetProjMatrix();       
  371.         matView = *g_Camera.GetViewMatrix();
  372.  
  373.         // Sort the scene by either material or mesh instance
  374.         SortRenderables();
  375.  
  376.         DXUT_BeginPerfEvent( DXUT_PERFEVENTCOLOR, L"Draw Code" );
  377.  
  378.         // Render each scene-level pass in turn
  379.         // A scene-level pass in different than an effects pass.
  380.         // Scene-level passes ensure that particular rendering orders are enforced, such as
  381.         // allowing for all transparent objects to be drawn last.
  382.         for( vector<pass>::iterator it_pass = g_passes.begin();
  383.              it_pass != g_passes.end();
  384.              it_pass++ )
  385.         {
  386.             pass& renderPass = *it_pass;
  387.  
  388.             // iterators, used to access the render queue for this pass
  389.             pass::iterator it = renderPass.begin();
  390.             pass::iterator it_begin;
  391.             pass::iterator it_end;
  392.  
  393.             while( renderPass.end() != it )
  394.             {
  395.                 UINT nPasses = 0;
  396.                 CEffect* pEffect = (*it).getEffect();
  397.                 
  398.                 // Render various materials using the same effect technique in batches.
  399.                 // Scan the list ahead to the next renderable not in this batch.
  400.                 for( it_end = it_begin = it; 
  401.                     (it_end != renderPass.end()) && ((*it_end).getEffect() == (*it).getEffect());
  402.                     it_end++ )
  403.                 {
  404.                     // no action needed here
  405.                     // following the loop, it_end will point to the element beyond the last
  406.                     // renderable sharing the same effect
  407.                 }
  408.  
  409.                 // Initialize the effect
  410.                 pEffect->getPointer()->Begin( &nPasses, D3DXFX_DONOTSAVESTATE );
  411.  
  412.                 CInstance *pInstance, *pLastInstance = NULL;
  413.                 CEffectInstance *pEffectInstance, *pLastEffectInstance = NULL;
  414.                 
  415.                 // All materials sharing the same effect will be grouped into the effect pass.
  416.                 // As-needed, the effect will be updated with material-specific (effect instance)
  417.                 // or mesh/object instance (transformation matrices).
  418.                 // Each time ID3DXEffect::BeginPass is invoked, state change commands to support
  419.                 // the effect are invoked.  
  420.                 for( UINT i = 0; i< nPasses; i++ )
  421.                 {
  422.                     // Effect-wide globals (scene-level info such as lighting, etc) should be
  423.                     // set here, before the pass begins
  424.                     //
  425.  
  426.                     // Initialize the pass
  427.                     pEffect->getPointer()->BeginPass( i );
  428.                     
  429.                     // Iterate across all renderables determined to share the same effect/technique 
  430.                     for( it = it_begin; it != it_end; it++ )
  431.                     {
  432.                         // The instance is needed to retrieve the world matrix for this object
  433.                         pInstance = (*it).getInstance();
  434.                         pEffectInstance = (*it).getEffectInstance();
  435.  
  436.                         // If the mesh object instance changed, the new object matrices
  437.                         // must be sent to the effect
  438.                         // The timestamp ensures that view and project matrices are not updated redundantly
  439.                         if( pLastInstance != pInstance )
  440.                             pEffect->SetMatrices( pInstance->getMatrix(), &matView, &matProj, nFrameTimeStamp );
  441.  
  442.                         // If the EffectInstance changed, the effect must be updated with the new
  443.                         // parameter values
  444.                         if( pLastEffectInstance != pEffectInstance )
  445.                             pEffectInstance->apply();
  446.  
  447.                         // Changing Effect Parameters mid-pass requires notifying ID3DXEffect
  448.                         pEffect->getPointer()->CommitChanges();
  449.  
  450.                         // The Effect has been fully updated -- Render the mesh subset!
  451.                         pInstance->getMeshObject()->getPointer()->DrawSubset( (*it).getSubset() );
  452.  
  453.                         pLastInstance = pInstance;
  454.                         pLastEffectInstance = pEffectInstance;
  455.                     }
  456.                     pEffect->getPointer()->EndPass( );
  457.                 }
  458.                 pEffect->getPointer()->End();
  459.             }
  460.         }
  461.  
  462.         DXUT_EndPerfEvent(); // end of draw code
  463.  
  464.         // To prevent invalidating any cached state values within the state manager
  465.         // the application may directly use its interface to change state.
  466.         // This ensures that the state manager is able to cache the
  467.         // last known state, in turn ensuring that a true state change is not mistakenly
  468.         // handled as if it were redundant.
  469.         if( g_pStateManager )
  470.         {
  471.             // The Effect State Manager is active, use it to set some states
  472.             g_pStateManager->SetPixelShader( NULL );
  473.         }
  474.         else
  475.         {
  476.             // Otherwise, it is safe to use the runtime directly
  477.             pd3dDevice->SetPixelShader( NULL );
  478.         }
  479.  
  480.         {
  481.             CDXUTPerfEventGenerator g( DXUT_PERFEVENTCOLOR, L"HUD / Stats" );
  482.             V( g_HUD.OnRender( fElapsedTime ) ); 
  483.             V( g_SampleUI.OnRender( fElapsedTime ) );
  484.             
  485.             RenderText( fTime );
  486.         }
  487.         
  488.         // If CDXUTDialog::OnRender or RenderText failed to restore any changed states
  489.         // (that were cached by the custom implementation of ID3DXEffectStateManager),
  490.         // it would be necessary to notifiy the state manager that the device state has
  491.         // been dirtied.  This would be done prior to rendering with the next effect.
  492.         // See CBaseStateManager::DirtyCachedValues.
  493.         // In this case, it may be safely assumed that the device state has been
  494.         // correctly restored between rendering of effects.
  495.  
  496.         V( pd3dDevice->EndScene() );
  497.     }
  498.     
  499.     nFrameTimeStamp++;
  500. }
  501.  
  502.  
  503.  
  504.  
  505. //--------------------------------------------------------------------------------------
  506. // Render the help and statistics text. This function uses the ID3DXFont interface for 
  507. // efficient text rendering.
  508. //--------------------------------------------------------------------------------------
  509. void RenderText( double fTime )
  510. {
  511.     // The helper object simply helps keep track of text position, and color
  512.     // and then it calls pFont->DrawText( m_pSprite, strMsg, -1, &rc, DT_NOCLIP, m_clr );
  513.     // If NULL is passed in as the sprite object, then it will work fine however the 
  514.     // pFont->DrawText() will not be batched together.  Batching calls will improves perf.
  515.     CDXUTTextHelper txtHelper( g_pFont, g_pSprite, 15 );
  516.  
  517.     // Output statistics
  518.     txtHelper.Begin();
  519.     txtHelper.SetInsertionPos( 2, 0 );
  520.     txtHelper.SetForegroundColor( D3DXCOLOR( 1.0f, 0.0f, 0.0f, 1.0f ) );
  521.     txtHelper.DrawTextLine( DXUTGetFrameStats() );
  522.     txtHelper.DrawTextLine( DXUTGetDeviceStats() );
  523.     if( g_bRuntimeDebug )
  524.         txtHelper.DrawTextLine( L"WARNING (perf): DEBUG D3D runtime detected!" );
  525.     if( g_bAppIsDebug )
  526.         txtHelper.DrawTextLine( L"WARNING (perf): DEBUG application build detected!" );
  527.     
  528.     
  529.     // Draw help
  530.     if( g_bShowHelp )
  531.     {
  532.         // Comparison of framerates is made fair by hiding help -- this avoids rendering unequal
  533.         // amounts of text depending on the state manager statistics
  534.         txtHelper.SetForegroundColor( D3DXCOLOR( 0.0f, 1.0f, 0.0f, 1.0f ) );
  535.         txtHelper.DrawTextLine( g_bSortByMaterial ? L"Sorted By:  Material" : L"Sorted By:  Instance" );
  536.         if( g_pStateManager )
  537.             txtHelper.DrawTextLine( g_pStateManager->EndFrameStats() );
  538.  
  539.         const D3DSURFACE_DESC* pd3dsdBackBuffer = DXUTGetBackBufferSurfaceDesc();
  540.         txtHelper.SetInsertionPos( 2, pd3dsdBackBuffer->Height-15*6 );
  541.         txtHelper.SetForegroundColor( D3DXCOLOR(1.0f, 0.75f, 0.0f, 1.0f ) );
  542.  
  543.         txtHelper.DrawTextLine( L"Controls:" );
  544.         txtHelper.DrawTextLine( L"Look: Left drag mouse\n"
  545.                                 L"Move: A,W,S,D or Arrow Keys\n" );
  546.  
  547.         txtHelper.SetInsertionPos( 250, pd3dsdBackBuffer->Height-15*5 );
  548.         txtHelper.DrawTextLine( L"Hide help: F1\n" 
  549.                                 L"Quit: ESC\n" );
  550.     }
  551.     else
  552.     {
  553.         txtHelper.SetForegroundColor( D3DXCOLOR( 1.0f, 1.0f, 1.0f, 1.0f ) );
  554.         txtHelper.DrawTextLine( L"Press F1 for help" );
  555.     }
  556.  
  557.     txtHelper.End();
  558. }
  559.  
  560.  
  561.  
  562.  
  563. //--------------------------------------------------------------------------------------
  564. // Before handling window messages, the sample framework passes incoming windows 
  565. // messages to the application through this callback function. If the application sets 
  566. // *pbNoFurtherProcessing to TRUE, then the sample framework will not process this message.
  567. //--------------------------------------------------------------------------------------
  568. LRESULT CALLBACK MsgProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, bool* pbNoFurtherProcessing )
  569. {
  570.     // Give the dialogs a chance to handle the message first
  571.     if( g_HUD.MsgProc( hWnd, uMsg, wParam, lParam ) )
  572.         return 0;
  573.     if( g_SampleUI.MsgProc( hWnd, uMsg, wParam, lParam ) )
  574.         return 0;
  575.  
  576.     // Pass all remaining windows messages to camera so it can respond to user input
  577.     g_Camera.HandleMessages( hWnd, uMsg, wParam, lParam );
  578.  
  579.     return 0;
  580. }
  581.  
  582.  
  583. //--------------------------------------------------------------------------------------
  584. // As a convenience, the sample framework inspects the incoming windows messages for
  585. // keystroke messages and decodes the message parameters to pass relevant keyboard
  586. // messages to the application.  The framework does not remove the underlying keystroke 
  587. // messages, which are still passed to the application's MsgProc callback.
  588. //--------------------------------------------------------------------------------------
  589. void CALLBACK KeyboardProc( UINT nChar, bool bKeyDown, bool bAltDown )
  590. {
  591.     if( bKeyDown )
  592.     {
  593.         switch( nChar )
  594.         {
  595.             case VK_F1: g_bShowHelp = !g_bShowHelp; break;
  596.         }
  597.     }
  598. }
  599.  
  600.  
  601. //--------------------------------------------------------------------------------------
  602. // Re-creates the device as being PURE HWVP, or (non-pure) HWVP, depending on the
  603. // current state.
  604. //--------------------------------------------------------------------------------------
  605. void TogglePure()
  606. {
  607.     DXUTDeviceSettings settings = DXUTGetDeviceSettings();
  608.     if( settings.BehaviorFlags & D3DCREATE_HARDWARE_VERTEXPROCESSING )
  609.     {
  610.         settings.BehaviorFlags ^= D3DCREATE_PUREDEVICE;
  611.         DXUTCreateDeviceFromSettings( &settings, true );
  612.     }
  613. }
  614.  
  615.  
  616. //--------------------------------------------------------------------------------------
  617. // Handles the GUI events
  618. //--------------------------------------------------------------------------------------
  619. void CALLBACK OnGUIEvent( UINT nEvent, int nControlID, CDXUTControl* pControl )
  620. {
  621.     WCHAR sz[100];
  622.     sz[0] = 0;
  623.  
  624.     switch( nControlID )
  625.     {
  626.         case IDC_TOGGLEFULLSCREEN: DXUTToggleFullScreen(); break;
  627.         case IDC_TOGGLEREF:        DXUTToggleREF(); break;
  628.         case IDC_CHANGEDEVICE:     DXUTSetShowSettingsDialog( !DXUTGetShowSettingsDialog() ); break;
  629.  
  630.  
  631.         case IDC_NUM_ROCKS:
  632.             g_nMaxRocksToRender =  g_SampleUI.GetSlider( IDC_NUM_ROCKS )->GetValue();
  633.  
  634.             _snwprintf( sz, 100, L"# Rocks: %d", g_nMaxRocksToRender ); sz[99] = 0;
  635.             g_SampleUI.GetStatic( IDC_NUM_ROCKS_STATIC )->SetText( sz );
  636.             QueueAndSortRenderables();
  637.             break;
  638.  
  639.  
  640.         case IDC_TOGGLEPURE:
  641.             TogglePure();
  642.             SetStateManager();
  643.             break;
  644.         
  645.         case IDC_STATEMANAGER:
  646.             g_bStateManger = g_SampleUI.GetCheckBox( IDC_STATEMANAGER )->GetChecked();
  647.             SetStateManager();
  648.             break;
  649.  
  650.         case IDC_MATERIALSORT:
  651.             g_bSortByMaterial = g_SampleUI.GetCheckBox( IDC_MATERIALSORT )->GetChecked();
  652.             g_bSortOrderDirty = true;
  653.             break;
  654.  
  655.         case IDC_BIND_CPU:
  656.             UINT n = g_SampleUI.GetSlider( IDC_BIND_CPU )->GetValue();
  657.             
  658.             // square the slider to provide an exponential response
  659.             g_nBindCPU = n*n; 
  660.  
  661.             _snwprintf( sz, 100, L"Bind CPU: %d", g_nBindCPU ); sz[99] = 0;
  662.             g_SampleUI.GetStatic( IDC_BIND_CPU_STATIC )->SetText( sz );
  663.             break;
  664.     }
  665. }
  666.  
  667.  
  668. //--------------------------------------------------------------------------------------
  669. // This callback function will be called immediately after the Direct3D device has 
  670. // entered a lost state and before IDirect3DDevice9::Reset is called. Resources created
  671. // in the OnResetDevice callback should be released here, which generally includes all 
  672. // D3DPOOL_DEFAULT resources. See the "Lost Devices" section of the documentation for 
  673. // information about lost devices.
  674. //--------------------------------------------------------------------------------------
  675. void CALLBACK OnLostDevice()
  676. {
  677.     // Notify device resources that the device has been lost
  678.     CTexture::LostDevice();
  679.     CEffect::LostDevice();
  680.     CMeshObject::LostDevice();
  681.  
  682.     if( g_pFont )
  683.         g_pFont->OnLostDevice();
  684.  
  685.     SAFE_RELEASE(g_pSprite);
  686.     
  687. }
  688.  
  689.  
  690. //--------------------------------------------------------------------------------------
  691. // This callback function will be called immediately after the Direct3D device has 
  692. // been destroyed, which generally happens as a result of application termination or 
  693. // windowed/full screen toggles. Resources created in the OnCreateDevice callback 
  694. // should be released here, which generally includes all D3DPOOL_MANAGED resources. 
  695. //--------------------------------------------------------------------------------------
  696. void CALLBACK OnDestroyDevice()
  697. {
  698.     SAFE_RELEASE( g_pStateManager );
  699.     SAFE_RELEASE(g_pFont);
  700.  
  701.     while( !g_vecInstances.empty() )
  702.     {
  703.         CInstance* pInstance = g_vecInstances.back();
  704.         vector<CInstance*>::iterator it_inst = remove( g_vecInstances.begin(), g_vecInstances.end(), pInstance );
  705.         g_vecInstances.erase( it_inst, g_vecInstances.end() );
  706.         SAFE_DELETE( pInstance );
  707.     }
  708. }
  709.  
  710.  
  711.  
  712. //--------------------------------------------------------------------------------------
  713. // Builds the scene from an x-file ('scene.x').
  714. // This x-file has been extended to supply mesh filenames and camera identifiers within
  715. // the frame hierarchy.
  716. //--------------------------------------------------------------------------------------
  717. HRESULT BuildSceneFromX( LPDIRECT3DDEVICE9 pd3dDevice )
  718. {
  719.     HRESULT hr;
  720.  
  721.     // LoadSceneFromX will populate vecFrameNodes with a list of frames and associated
  722.     // objects (meshes, cameras) to be instantiated.
  723.     vector<FRAMENODE> vecFrameNodes;
  724.     
  725.     WCHAR wszPath[MAX_PATH];
  726.     V_RETURN( DXUTFindDXSDKMediaFileCch( wszPath, MAX_PATH, L"scene.x" ) );
  727.  
  728.     // Objects can be added to the scene by invoking:
  729.     // CreateInstance( device, meshfilename, transform matrix, render pass );
  730.     // Here, the scene is read in from an x-file that has been extended to support embedding of mesh filenames.
  731.     // Following the LoadSceneFromFile, container vecFrameNodes will contain the collapsed hierarchy.
  732.     V_RETURN( LoadSceneFromX( vecFrameNodes, wszPath ) );
  733.  
  734.     // Having loaded (and collapsed) the frame hierarchy, any required meshes are instantiated.
  735.     for( vector<FRAMENODE>::iterator it = vecFrameNodes.begin();
  736.          it != vecFrameNodes.end();
  737.          it++ )
  738.     {
  739.         // retrieve the mesh sub-container for the frame
  740.         vector<MESH_REFERENCE>& vecMeshes = (*it).meshes;
  741.  
  742.         // each mesh found within the frame is iterated through
  743.         for( vector<MESH_REFERENCE>::iterator it_meshes = vecMeshes.begin();
  744.              it_meshes != vecMeshes.end();
  745.              it_meshes++ )
  746.         {
  747.             // The filename string must be converted
  748.             WCHAR wszFileName[MAX_PATH];
  749.             MultiByteToWideChar( CP_ACP, 0, (*it_meshes).szFileName, -1, wszFileName, MAX_PATH );
  750.             wszFileName[ MAX_PATH -1 ] = 0;
  751.             
  752.             // Create in instance of the mesh.
  753.             // The resource-management classes ensure that multiple references to the same
  754.             // mesh are not loaded more than once.
  755.             // (The same is true for resources used by the mesh -- textures and effects)
  756.             V_RETURN( CreateInstance( pd3dDevice, wszFileName, &(*it).mat, (*it_meshes).dwRenderPass ) );
  757.         }
  758.         
  759.         // If a camera reference was found, Reset the App camera
  760.         if( !(*it).cameras.empty() && g_bResetCamera )
  761.         {
  762.             // Extract the camera class view params from the camera world matrix
  763.             // Note: Assumes NO pitch, NO roll
  764.             D3DXVECTOR3 Eye( (*it).mat._41, (*it).mat._42, (*it).mat._43 );
  765.             D3DXVECTOR3 At( Eye );
  766.             At = Eye - D3DXVECTOR3( (*it).mat._13, (*it).mat._23, (*it).mat._33 ) ;
  767.  
  768.             g_Camera.SetViewParams( &Eye, &At );
  769.             g_Camera.SetScalers( (*it).cameras[0].fRotationScaler, (*it).cameras[0].fMoveScaler );
  770.  
  771.             // Do not reset the camera if re-building the scene across a defice change
  772.             g_bResetCamera = false;
  773.         }
  774.     }
  775.  
  776.     return S_OK;
  777. }
  778.  
  779.  
  780. //--------------------------------------------------------------------------------------
  781. // Renderable sub-items (mesh subsets with their associated materials) are maintained
  782. // in a render queue.  This queue allows for sorting by material or instance.
  783. //--------------------------------------------------------------------------------------
  784. void QueueAndSortRenderables()
  785. {
  786.     // All render queue will be regenerated - ensure that the old queues are cleared
  787.     g_passes.clear();
  788.  
  789.     UINT nRocksCount = 0;
  790.  
  791.     // Add each mesh/object instance into the correct render queue
  792.     for( vector<CInstance*>::iterator it = g_vecInstances.begin();
  793.          it!= g_vecInstances.end();
  794.          it++ )
  795.     {
  796.         CInstance* pInstance = *it;
  797.         UINT nRenderPass = pInstance->getRenderPass();
  798.  
  799.         // ensure that enough render passes exist
  800.         if( nRenderPass >= g_passes.size() )
  801.             g_passes.resize( nRenderPass+1 );
  802.  
  803.         // Constrain the number of rocks in the scene on the maximum limit
  804.         if( !_wcsicmp(pInstance->getMeshObject()->getName().c_str(), L"Rock.x")
  805.             && ++nRocksCount > g_nMaxRocksToRender )
  806.             continue;
  807.  
  808.         // Add this object's renderables to the render queue
  809.         DWORD dwSubsets = (DWORD) pInstance->getMeshObject()->getSubsetCount();
  810.         for( DWORD dw = 0; dw< dwSubsets; dw++ )
  811.         {
  812.             g_passes[nRenderPass].push_back( CRenderable( pInstance, dw ) );
  813.         }
  814.     }
  815.  
  816.     if( g_nMaxRocksToRender > nRocksCount )
  817.         g_nMaxRocksToRender = nRocksCount;
  818.  
  819.     // Update the slider to reflect the true number of rocks
  820.     WCHAR sz[100];
  821.     g_SampleUI.GetSlider( IDC_NUM_ROCKS )->SetRange( 0, nRocksCount );
  822.     _snwprintf( sz, 100, L"# Rocks: %d", g_nMaxRocksToRender ); sz[99] = 0;
  823.     g_SampleUI.GetStatic( IDC_NUM_ROCKS_STATIC )->SetText( sz );
  824.  
  825.     g_bSortOrderDirty = true;
  826. }
  827.  
  828.  
  829. //--------------------------------------------------------------------------------------
  830. // Re-sort the list of renderables (mesh subsets)
  831. // It should be more efficient to render groups of similar materials in batches
  832. // (g_bSortByMaterial).  This should serve to minimize the number of effect changes,
  833. // which in turn should minimize the number of state changes required to render the
  834. // scene.
  835. //--------------------------------------------------------------------------------------
  836. void SortRenderables()
  837. {
  838.     if( g_bSortOrderDirty )
  839.     {
  840.         // Each queue is sorted by material (effect) or instance
  841.         for( vector<pass>::iterator it = g_passes.begin();
  842.             it != g_passes.end();
  843.             it++ )
  844.         {
  845.             sort( (*it).begin(),
  846.                 (*it).end(),
  847.                 g_bSortByMaterial ? greaterMaterial : greaterInstance );
  848.         }
  849.     }
  850.  
  851.     g_bSortOrderDirty = false;
  852. }
  853.  
  854.  
  855. //--------------------------------------------------------------------------------------
  856. // Helper to create an instance of a mesh to be rendered in the scene
  857. //--------------------------------------------------------------------------------------
  858. HRESULT CreateInstance( LPDIRECT3DDEVICE9 pDevice, LPCWSTR wszFileName,
  859.                         D3DXMATRIX* pWorld, UINT nRenderPass, CInstance** ppInstance )
  860. {
  861.     HRESULT hr;
  862.  
  863.     CMeshObject* pMesh = NULL;
  864.     V_RETURN( CMeshObject::Create( pDevice, wszFileName, &pMesh ) );
  865.  
  866.     CInstance* pInstance = new CInstance( pMesh, pWorld, nRenderPass );
  867.     if( NULL == pInstance )
  868.         throw bad_alloc();
  869.  
  870.     g_vecInstances.push_back( pInstance );
  871.  
  872.     if( ppInstance )
  873.         *ppInstance = pInstance;
  874.  
  875.     return S_OK;
  876. }
  877.  
  878.  
  879.  
  880. //--------------------------------------------------------------------------------------
  881. // If g_gStateManger is 'true', a custom handler for D3DX Effect state changes will be
  882. // activated.  Otherwise, any active custom handler will be de-activated.
  883. //--------------------------------------------------------------------------------------
  884. void SetStateManager( )
  885. {
  886.     // Release any reference count on the current custom state manager.
  887.     // If there is no current custom state manager, g_pStateManager will be NULL.
  888.     SAFE_RELEASE( g_pStateManager );
  889.  
  890.     // If the application no longer wishes to implement state management, it should
  891.     // invoke SetStateManger(NULL) for each effect.
  892.     // This value (NULL) becomes the default case, depending on the application settings.
  893.     CStateManagerInterface* pManager = NULL;
  894.     
  895.     // Create the state manager
  896.     if( g_bStateManger )
  897.         pManager = CStateManagerInterface::Create( DXUTGetD3DDevice() );
  898.  
  899.     // Set this state manager as active across all loaded effects
  900.     // D3DX will release the old state manager for each effect, and invoke AddRef() to
  901.     // increment the reference count of the new state manager.
  902.     CEffect::SetStateManager( pManager );
  903.  
  904.     // Because D3DX maintains a reference count on this state manager while in use,
  905.     // it is possible to release the app's reference here.  Doing so allows the app
  906.     // to 'forget' about the state manager and let d3dx manager the lifetime of it.
  907.     // SAFE_RELEASE( pManager );
  908.  
  909.     // However, in this case it is desireable to maintain the pointer to the state
  910.     // manager, such that it can be queried for statistics while in use.
  911.     g_pStateManager = pManager;
  912. }
  913.  
  914.  
  915.